
var status_str;

function createXMLHttpRequest(cbFunc)
{
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
//		XMLhttpObject.overrideMimeType("text/plain; charset=GB2312");
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	return XMLhttpObject;
}


function statusCheck()
{
	if ((httpObj.readyState == 4) && (httpObj.status == 200))
	{
		displayData();
	}
}
function loadTextFile()
{
	var pattern = /(.*id=)/;
	var pattern2 = /\&(.*)/;
	var site = getCookie("xys_site");

	var id ="";
	var id0 ="";
	var txt ="";


	httpObj = createXMLHttpRequest(displayData);
	if (httpObj )
	{
		id=document.URL;
		id = id.replace(pattern, "");
		id0 = id.replace(pattern2, "");


		txt = "Loading file http://www.xys.org/xys/" + id0 + " ...( Maximum 30 seconds ) <br><br>";
		txt += "(It may take a little long time for loading if the file is big. You can <a href='" + site+"/xys/"+id0+"'>open it directly</a> if you can visit the http://www.xys.org or its mirror site.)<br>";

		//txt += " <br> <font size=4>IE is so slow, why not Firefox? <a href='http://www.xys-reader.org/blogs/sys_admin/2007/12/09/firefox/' target='_blank'>Get it Here.</a></font> <br>";

		//document.getElementById("result").innerHTML = txt; 
		status_str=txt;

		httpObj.open("GET","./d_fetch.php?id="+id,true);
		httpObj.send(null);
		timerID = setTimeout("httpMaybeLongTime()",3000); 
	}
}
function displayData()
{
	if (httpObj.readyState == 4)
	{
		try {
			statusCode[""+httpObj.status]();
		}catch(e){
			return;
		}
	}
}

statusCode = new Array();
statusCode["200"] = function (){

//	var pattern = /(http:\/\/[^\(\)<>\n\s]*)/ig;
 	//var pattern = /(http:\/\/[a-zA-Z0-9~;,:=!%#@&_\-\.\/\?]*)\n?[^(http)\s]([a-zA-Z0-9~;,:=!%#@&_\-\.\/\?]+)/g;
 	var pattern = /(http:\/\/[a-zA-Z0-9~;,:=!%#@&_\-\.\/\?+]*)/g;
 	var pattern_xys = /(XYS\d{8})/g;
 	var pattern_space = /\.\s/g;
	var txt =  httpObj.responseText; 
	var i=0;
	var newtxt ="";
	var newtxt2 ="";

	//txt = utf8to16(base64decode(txt));

	//newtxt2=  txt.charCodeAt(0); //Notice: There appears a very strange character "NL" char(10).

	for(i=2;i<txt.length;i++){
	    newtxt += String.fromCharCode( (((txt.charCodeAt(i)-43-txt.charCodeAt(1) - (i-1)*(i-1) )%82 + 82 )%82) + 43 );
	    //newtxt += String.fromCharCode( (((txt.charCodeAt(i)-43-txt.charCodeAt(0) - (i)*(i) )%82 + 82 )%82) + 43 );
	}

	txt = utf8to16(base64decode(newtxt));
	//txt = txt.replace(pattern_space, ".");
	//txt = txt.replace(pattern, "<a target=\"_parent\" href=\"$1\">$1</a>");
	txt = txt.replace(pattern_xys, "<a target=\"_parent\" href=\"http://www.xys-reader.org/xys/$1\">$1</a>");
 	document.getElementById("disp").innerHTML = ""+ txt+ ""+"<hr size=1>PROUDLY SUPPORTED BY XYS-READER.ORG"+newtxt2;
	document.getElementById("result").innerHTML = ""; 
 	document.getElementById("disp").focus();
	set_site();
	top.down.focus();
}
statusCode["401"] = function (){ document.getElementById("disp").innerHTML = "错误401"; }
statusCode["403"] = function (){ document.getElementById("disp").innerHTML = "错误403"; }
statusCode["404"] = function (){ document.getElementById("disp").innerHTML = "错误404"; }
statusCode["500"] = function (){ document.getElementById("disp").innerHTML = "错误500"; }

function httpMaybeLongTime()
{
	if(document.getElementById("disp").innerHTML == ""){
	   document.getElementById("result").innerHTML = status_str; 
	}
	timerID = setTimeout("httpAbort()",35000); 
}

function httpAbort()
{
	httpObj.abort();
	txt = "Timeout ....<br>";
	if(document.getElementById("disp").innerHTML == ""){
	   document.getElementById("result").innerHTML = txt; 
	}
}

